<template>
	<!-- 企业列表 -->
	<div class="content" v-if="!is_detail">
		<div class="content-top">
			企业管理_企业查询
		</div>
		<div class="content-operation">
			<el-button type="primary" icon="el-icon-plus" @click='is_add=true' v-if='has_add'>添加企业</el-button>
		</div>
		<div class="content-search">
			<el-form :inline="true" class="demo-form-inline" style="margin-top: 20px;">
				<el-form-item label="企业编码">
					<el-input v-model="enter_id" placeholder="企业编码" clearable></el-input>
				</el-form-item>
				<el-form-item label="企业名称">
					<el-input v-model="enter_name" placeholder="企业名称" clearable></el-input>
				</el-form-item>
				<el-form-item label="企业类型">
					<el-select v-model="enter_type" clearable placeholder="请选择">
						<el-option v-for="item in dict.enterpriseType" :key="item.value" :label="item.type" :value="item.value">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click='search' icon="el-icon-search">搜索</el-button>
				</el-form-item>
			</el-form>
		</div>
		<div class="content-table">
			<el-table :data="tableData" show-header stripe style="width: 100%;">
				<el-table-column prop="enterpriseName" label="企业名称">
				</el-table-column>
				<el-table-column prop="enterpriseCode" label="企业编码">
				</el-table-column>
				<el-table-column prop="enterpriseType" label="企业类型">
					<template slot-scope="scope">
						<span v-if="scope.row.enterpriseType == item.value" v-for='item in dict.enterpriseType'>{{item.type}}</span>
					</template>
				</el-table-column>
				<el-table-column prop="contactPersonName" label="联系人姓名">
				</el-table-column>
				<el-table-column prop="contactPersonPhone" label="联系人电话">
				</el-table-column>
				<!-- <el-table-column
            prop="enterpriseAddress"
            label="企业地址"
            width="400">
          </el-table-column> -->
				<!-- <el-table-column
            prop="enterpriseStatus"
            label="企业状态"
            >
			<template slot-scope="scope">
				<span v-if="scope.row.enterpriseStatus == item.value" v-for='item in dict.enterpriseStatus' >{{item.type}}</span>
			</template>
          </el-table-column> -->

				<!-- <el-table-column
		    prop="openingBank"
		    label="开户行"
		    width="220">
		  </el-table-column>
		  <el-table-column
		    prop="accountName"
		    label="开户姓名"
		    width="120">
		  </el-table-column>
		  <el-table-column
		    prop="bankNum"
		    label="银行账号"
		    width="220">
		  </el-table-column> -->
				<el-table-column fixed="right" label="操作" width="100">
					<template slot-scope="scope">
						<el-button @click="showdetail(scope.row)" v-if="has_edit" type="text" size="small"><i class="el-icon-view"></i></el-button>
						<el-button @click="handleClick(scope.row)" v-if="has_edit" type="text" size="small"><i class="el-icon-edit"></i></el-button>
						<el-button @click="handleClick1(scope.row)" v-if="has_del" type="text" size="small"><i class="el-icon-delete"></i></el-button>
					</template>
				</el-table-column>
			</el-table>
		</div>
		<div class="content-page">
			<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="page"
			 :page-size=limit layout="prev, pager, next, jumper" :total=total>
			</el-pagination>
		</div>
		<template v-if="is_add">
			<div class="all-screen"></div>
			<div class='add-box'>
				<CompanyAdd @saveAdd='saveAdd'></CompanyAdd>
			</div>
		</template>
		<template v-if="is_edit">
			<div class="all-screen"></div>
			<div class='add-box'>
				<CompanyEdit @saveEdit='saveEdit' :id='id'></CompanyEdit>
			</div>
		</template>
	</div>
	<div v-else class="content">
		<div class="content-top">
			企业管理_企业详情
		</div>
		<div class="enter-title">
			<div class="enter-title-left">基本信息</div>
			<div class="enter-title-right" @click="is_edit = true"><i class="el-icon-edit"></i>修改</div>
		</div>
		<div class="enter-detail">
			<el-row>
				<el-col :span="8">企业名称：{{info.enterpriseName}}</el-col>
				<el-col :span="8">企业编码：{{info.enterpriseCode}}</el-col>
				<el-col :span="8">企业类型：<template v-if="item.value == info.enterpriseType" v-for='item in dict.enterpriseType'>{{item.type}}
					</template></el-col>
			</el-row>
			<el-row>
				<el-col :span="24">企业地址：{{info.enterpriseAddress}}</el-col>
				
			</el-row>
			<el-row>
				<el-col :span="8">联系人姓名：{{info.contactPersonName}}</el-col>
				<el-col :span="8">联系人电话：{{info.contactPersonPhone}}</el-col>
			</el-row>
			<el-row>
				<el-col :span="8">开户行：{{info.openingBank}}</el-col>
				<el-col :span="8">银行账号：{{info.bankNum}}</el-col>
				<el-col :span="8">户名：{{info.accountName}}</el-col>
			</el-row>
			<el-row>
				<el-col :span="24">创建时间：{{info.createDate}}</el-col>
			</el-row>
		</div>

		<div class="enter-title">
			<center>
				<el-button type="danger" @click='handleClick1({"id":id})' icon="el-icon-delete"></el-button>
				<el-button type="primary" @click='show_list'><i class="el-icon-back"></i></el-button>
			</center>
		</div>
		<template v-if="is_edit">
			<div class="all-screen"></div>
			<div class='add-box'>
				<CompanyEdit @saveEdit='saveEdit' :id='id'></CompanyEdit>
			</div>
		</template>
	</div>
</template>

<script>
	import CompanyAdd from '@/components/company/CompanyAdd.vue'
	import CompanyEdit from '@/components/company/CompanyEdit.vue'
	import Global from '@/components/Global.vue'
	let _this;
	export default {
		components: {
			CompanyAdd: CompanyAdd,
			CompanyEdit: CompanyEdit,
		},
		props: ['power'],
		data() {
			return {
				enter_id: '',
				enter_type: '',
				enter_name: '',
				id: 'id',
				is_add: false,
				is_edit: false,
				is_detail: false,
				tableData: [],
				limit: 10,
				page: 1,
				total: 1,
				dict: '',
				has_add: false,
				has_edit: false,
				has_del: false,
				info: ''

			}
		},
		created() {
			_this = this;
		},
		mounted() {
			this.check_power();
			this.get_dict();
			this.get_list();
		},
		methods: {
			show_list(){
				this.is_detail = false;
				this.info = {};
			},
			check_power() {
				this.power.forEach(function(element) {
					if (element.permissionName.indexOf("添加") != -1) _this.has_add = true;
					if (element.permissionName.indexOf("编辑") != -1) _this.has_edit = true;
					if (element.permissionName.indexOf("删除") != -1) _this.has_del = true;
				});
			},
			get_dict() {
				this.$axios.get(this.$global.serverIp + 'admin/dict/findDict?keyword=sysEnterprise')
					.then(function(res) {
						if (res != '')
							_this.dict = res.data;
					})

			},
			get_list() {
				this.$axios.post(this.$global.serverIp + 'jianeng-admin/sysEnterprise/findPage', {
						"limit": _this.limit + "",
						"page": _this.page + "",
						"enterpriseCode": _this.enter_id,
						"enterpriseName": _this.enter_name,
						"enterpriseType": _this.enter_type
					})
					.then(function(res) {
						if (res != '') {
							_this.tableData = res.data.list;
							_this.total = res.data.totalCount;
							_this.page = res.data.currPage;
						}

					})

			},
			saveAdd(type) {
				if (type == 2) {
					this.get_list();
				}
				this.is_add = false;
			},

			saveEdit(type) {
				if (type == 2) {
					this.get_list();
					this.is_detail = false;
				}
				this.is_edit = false;
			},
			search() {
				this.page = 1;
				this.get_list();
			},

			handleSizeChange(val) {
				console.log(`每页 ${val} 条`);
			},
			handleCurrentChange(val) {
				this.page = val;
				this.get_list();
				console.log(`当前页: ${val}`);
			},
			showdetail(row) {
				this.id = row.id;
				this.get_info();
				this.is_detail = true;
			},
			get_info() {
				this.$axios.get(this.$global.serverIp + 'admin/sysEnterprise/detail/' + this.id)
					.then(function(res) {
						if (res != '') {
							_this.info = res.data;
							_this.info.enterpriseType = _this.info.enterpriseType+"";
						}
					})
			},
			handleClick(row) {
				console.log(row.name);
				this.id = row.id;
				this.is_edit = true;

			},
			handleClick1(row) {
				this.delopen(row);
			},
			delopen(row) {
				this.$confirm('此操作将删除该企业, 是否继续?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					$.ajax({
						url: Global.serverIp + 'jianeng-admin/sysEnterprise/delete',
						data: '["' + row.id + '"]',
						xhrFields: {
							withCredentials: true
						},
						beforeSend: function(request) {
							request.setRequestHeader("Authorization", 'Bearer ' + localStorage.getItem('token'));
						},
						contentType: 'application/json;charset=utf-8',
						dataType: 'json',
						type: 'delete',
						success: function(res) {
							if (res.code == Global.success_code) {
								console.log(res);
								_this.$popup.open("删除成功");
								_this.is_detail = false;
								_this.get_list();
							} else {
								_this.$popup.open(res.msg);
							}
						},
						error: function(res) {
							if (res.responseJSON.code == '040005') {
								_this.$popup.open("登录过期");
							} else {
								_this.$popup.open("请求失败");
							}
						}
					})
				}).catch(() => {
					this.$message({
						type: 'info',
						message: '已取消删除'
					});
				});
			},
			
		},

	}
	import '@/assets/css/common.css'
</script>
<style>
	.enter-title {
		margin: 26px 0px 0px 30px;
		overflow: hidden;
		font-size: 16px;
		width: 1088px;
	}
	
	.enter-title-left {
		float: left;
		color: #606B82;
		margin-bottom: 16px;
	}
	
	.enter-title-right {
		float: right;
		color: #FD943F;
		margin-bottom: 10px;
	}
	
	.enter-title-right:hover {
		cursor: pointer;
		opacity: 0.3;
	}
	
	.enter-detail {
		width: 1088px;
		height: 300px;
		background-color: #F2F6FA;
		border: 1px solid #C9CDD1;
		padding-left: 30px;
		box-sizing: border-box;
		margin-left: 30px;
		padding-top: 40px;
	}
	
	.enter-detail .el-row {
		margin-bottom: 30px;
		font-size: 14px;
		color: #2D384C;
	}
	
</style>
